package com.fp.voiceshoper.adapters;

import java.util.List;

import android.content.Context;
import android.text.Spannable;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fp.fpShoper.R;
import com.fp.voiceshoper.items.ShopItemData;

public class ShopItemAdapter  extends ArrayAdapter<ShopItemData> {

    private final Context context;
    private List<ShopItemData> list;
    
    private Boolean favMode=false;
    
    public int getCount() {
        return list.size();
    }

    /**
     * Since the data comes from an array, just returning the index is
     * sufficient to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     *
     * @see android.widget.ListAdapter#getItem(int)
     */
    public ShopItemData getItem(int position) {
        return list.get(position);
    }

    /**
     * Use the array index as a unique id.
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }
    
    public void onRemove(int which) {
		if (which < 0 || which > list.size()) return;		
		list.remove(which);
	}

    public void update(int from,ShopItemData temp){
    	list.remove(from);
		list.add(from,temp);
    }
    
	public void onDrop(int from, int to) {
		ShopItemData temp = list.get(from);
		list.remove(from);
		list.add(to,temp);
	}
    
	public ShopItemAdapter(Context context, Boolean favMod,  List<ShopItemData> list) {
		super(context, R.layout.shopitem, list);
		  this.context = context;
	        this.list = list;
	        this.favMode=favMod;
		// TODO Auto-generated constructor stub
	}
	  private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
	 @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
		 
	        LayoutInflater inflater = (LayoutInflater) context
	                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        View rowView = inflater.inflate(R.layout.shopitem, parent, false);
	        
	        TextView textView = (TextView) rowView.findViewById(R.id.label);
	        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
	        
	        int show=1;
	        if (favMode) show--;
	        if (list.get(position).getCount()>show){
	        	String[] mystr=context.getResources().getStringArray(R.array.item_type);
	        	String type=mystr[list.get(position).getType()];
	        	String str=String.format("%s  (%d %s)",list.get(position).getName(),list.get(position).getCount(),type);
	        	textView.setText(str);
	        }
	        else{
	        	textView.setText(list.get(position).getName());
	        }
	      
	        if (list.get(position).isBuyed()) {
	            imageView.setImageResource(R.drawable.icon_circleselect);
	            textView.setText(textView.getText(), TextView.BufferType.SPANNABLE);
				Spannable spannable = (Spannable) textView.getText();
				spannable.setSpan(STRIKE_THROUGH_SPAN, 0, textView.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
	            
	        } else {
	            imageView.setImageResource(R.drawable.icon_shopping);
	        }
	        return rowView;
	    }
	 
	 private Boolean check(int pos){
		 if (pos>=0 && pos<list.size()) return true;
		 return false;
	 }
	 
	   public void addItem(final ShopItemData item)
	    {
		   list.add(item);
	 
	       notifyDataSetChanged();
	    }
	   
	   public void setBuy(int pos, Boolean buy){
		   if (!check(pos)) return;
		   list.get(pos).setBuyed(buy);
		   
		   notifyDataSetChanged();
	   }
	   
	   public void delItem(int pos){
		   if (!check(pos)) return;
		   list.remove(pos);
		   notifyDataSetChanged();
	   }
	   
	   public void updateItem(ShopItemData item, int id){
		   
	   }
	 
}
